package cn.com.dhcc.turbo.service.exch.cmdata;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.dao.CrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.service.AbstractCrudService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.consts.NodeInfoParam;
import cn.com.dhcc.app.pub.core.tag.AppTagEl;
import cn.com.dhcc.turbo.dao.exch.RecvCmDataDao;
import cn.com.dhcc.turbo.entity.exch.RecvCmData;

@Service
public class RecvCmDataService extends AbstractCrudService<RecvCmData> {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.R);

	@Autowired
	private RecvCmDataDao dao;

	@Override
	public CrudDao<RecvCmData> getCrudDao() {
		return dao;
	}

	/**
	 * 获取尚未发送的给接收系统的数据列表
	 * 跨节点,跨网类型为非内网到外网
	 * @return
	 * @throws NiceException
	 * @throws ServiceException 
	 */
	public List<RecvCmData> getUnsentList() throws NiceException, ServiceException {
		boolean flag=AppTagEl.isInnerNet(NodeInfoParam.NODE_NET_TYPE);
		WhereCondition parameter = new WhereCondition();
		if (flag) {
			parameter.where().notEq("send_status", AppConst.SendStatus.FAIL.value())
			.and().notEq("send_status", AppConst.SendStatus.SENT.value())
			.and().notEq("send_status", AppConst.SendStatus.REMOTE_SENDING.value())
			
			//.and().eq("CROSS_NODE", AppConst.YES)
			.and().notEq("CROSS_NET", AppConst.CrossNetType.INNER_OUT_NETWORK.value());
		}else{
			parameter.where().notEq("send_status", AppConst.SendStatus.FAIL.value())
			.and().notEq("send_status", AppConst.SendStatus.SENT.value())
			.and().notEq("send_status", AppConst.SendStatus.REMOTE_SENDING.value())
			.and().eq("CROSS_NODE", AppConst.YES);
		}
		return this.list(parameter);
	}

	/**
	 * 批量更新数据发送状态
	 * @param dataList
	 */
	public int updateSendStatus(List<RecvCmData> dataList, String status) throws NiceException {
		try {
			return dao.updateSendStatus(dataList, status);
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}
	
	
	/**
	 * 获取发送节点列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getSendNodeList() throws NiceException {
		try {
			return dao.getSendNodeList();
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}

	/**
	 * 获取接收节点列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getRecvNodeList() throws NiceException {
		try {
			return dao.getRecvNodeList();
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}


	/**
	 * 获取发送适配器列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getSendAdapterList() throws NiceException {
		try {
			return dao.getSendAdapterList();
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}

}
